// Copyright (c) 2023 坚果派
// 公众号：nutpi
// 电话：17752170152
// 官网：https://www.nutpi.net/
// All rights reserved.

import 'package:flutter/material.dart';

class BMICalculator {
  /// 计算BMI值
  static double calculateBMI(double weight, double heightCm) {
    final heightM = heightCm / 100; // 转换为米
    return weight / (heightM * heightM);
  }

  /// 获取BMI评级
  static String getBMICategory(double bmi) {
    if (bmi < 18.5) {
      return '偏瘦';
    } else if (bmi >= 18.5 && bmi < 24.0) {
      return '正常';
    } else if (bmi >= 24.0 && bmi < 28.0) {
      return '超重';
    } else {
      return '肥胖';
    }
  }

  /// 获取BMI对应的颜色
  static Color getBMIColor(double bmi) {
    if (bmi < 18.5) {
      return Colors.blue; // 偏瘦
    } else if (bmi >= 18.5 && bmi < 24.0) {
      return Colors.green; // 正常
    } else if (bmi >= 24.0 && bmi < 28.0) {
      return Colors.orange; // 超重
    } else {
      return Colors.red; // 肥胖
    }
  }

  /// 获取健康体重范围 (基于身高)
  static Map<String, double> getHealthyWeightRange(double heightCm) {
    final heightM = heightCm / 100;
    final minWeight = 18.5 * heightM * heightM;
    final maxWeight = 23.9 * heightM * heightM;
    return {
      'min': double.parse(minWeight.toStringAsFixed(1)),
      'max': double.parse(maxWeight.toStringAsFixed(1)),
    };
  }

  /// 获取BMI改善建议
  static String getBMIRecommendation(
      double bmi, double weight, double heightCm) {
    final healthyRange = getHealthyWeightRange(heightCm);

    if (bmi < 18.5) {
      final weightToGain = healthyRange['min']! - weight;
      return '您的体重偏轻。健康的体重范围是 ${healthyRange['min']}kg - ${healthyRange['max']}kg。'
          '建议在医生指导下，增加约 ${weightToGain.toStringAsFixed(1)}kg 以达到健康体重范围。';
    } else if (bmi >= 18.5 && bmi < 24.0) {
      return '恭喜！您的体重处于健康范围内 (${healthyRange['min']}kg - ${healthyRange['max']}kg)。'
          '继续保持健康的生活方式以维持当前体重。';
    } else if (bmi >= 24.0 && bmi < 28.0) {
      final weightToLose = weight - healthyRange['max']!;
      return '您的体重超重。健康的体重范围是 ${healthyRange['min']}kg - ${healthyRange['max']}kg。'
          '建议减轻约 ${weightToLose.toStringAsFixed(1)}kg 以达到健康体重范围。'
          '适当控制饮食并增加身体活动。';
    } else {
      final weightToLose = weight - healthyRange['max']!;
      return '您的体重属于肥胖。健康的体重范围是 ${healthyRange['min']}kg - ${healthyRange['max']}kg。'
          '建议在医生指导下，减轻约 ${weightToLose.toStringAsFixed(1)}kg 以达到健康体重范围。'
          '制定合理的饮食计划和运动方案，控制能量摄入，增加身体活动。';
    }
  }
}
